package com.adnaan.backend.controller.wechat;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.json.JSONObject;
import com.adnaan.backend.common.annotation.RestControllerMapping;
import com.adnaan.backend.common.model.Result;
import com.adnaan.backend.entity.Article;
import com.adnaan.backend.entity.dto.BannerDto;
import com.adnaan.backend.service.ArticleRecommendService;
import com.adnaan.backend.service.BannerService;
import com.adnaan.backend.utils.HttpJsonRequestUtil;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * author: adnaan
 * create: 2025-02-17 14:01
 * Description:
 */
@Tag(name = "通用展示数据", description = "通用展示数据")
@RestControllerMapping("/showData")
@ApiSupport(author = "adnaan", order = 8)
@Slf4j
public class ShowDataController {

    @Resource
    private HttpJsonRequestUtil httpJsonRequestUtil;

    @Resource
    private BannerService bannerService;

    @Resource
    private ArticleRecommendService articleRecommendService;

    @Operation(summary = "获取天气数据")
    @GetMapping("/weather")
    public Result<Object> getWeather(@RequestParam String city) {
        String url = "http://shanhe.kim/api/za/tianqi.php?city=" + city + "&type=json";
        String jsonResponse = httpJsonRequestUtil.get(url, String.class);
        JSONObject result = new JSONObject(jsonResponse);
        int code = result.getInt("code");
        String msg = result.getStr("text");
        if (code != 1) {
            return Result.error(msg);
        }
        log.info("result: {}", result);
        return Result.success("获取天气数据成功", result.get("data"));
    }

    @Operation(summary = "获取轮播图")
    @GetMapping("/banner")
    public Result<List<BannerDto>> getBanner() {
        List<BannerDto> banners = bannerService.getBanners();
        return Result.success(banners);
    }

    @Operation(summary = "获取推荐文章")
    @GetMapping("/recommend")
    public Result<List<Article>> getRecommendedArticles(@Parameter(description = "推荐数量") @RequestParam(defaultValue = "5") Integer limit) {
        Long userId = StpUtil.getLoginIdAsLong();
        List<Article> recommendedArticles = articleRecommendService.recommendArticles(userId, limit);
        return Result.success(recommendedArticles);
    }
}
